package com.yxjme.stateviewlayoutlibrary;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CommonStateView extends LinearLayout {

    private ImageView icon ;
    private TextView tvContent ;

    public CommonStateView(Context context) {
        this(context,null);
    }

    public CommonStateView(Context context,  AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CommonStateView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);
        initView();
    }


    private void initView() {
        icon = new ImageView(getContext()) ;
        icon.setImageResource(R.mipmap.loading_jh_1);
        addView(icon);
        tvContent = new TextView(getContext());
        tvContent.setTextSize(16);
        tvContent.setGravity(Gravity.CENTER);
        tvContent.setTextColor(Color.BLACK);
        tvContent.setAlpha(0.5f);
        tvContent.setPadding(0,20,0,0);
        tvContent.setHighlightColor(Color.TRANSPARENT);
        addView(tvContent);
    }



    public void setColor(int color){
        tvContent.setTextColor(color);
    }

    /**
     * @param t
     */
    public void setText(String t){
        tvContent.setAlpha(1);
        tvContent.setText(t);
        tvContent.setVisibility(TextUtils.isEmpty(t)?GONE:VISIBLE);
    }

    /**
     * @param t
     */
    public void setText(SpannableString t){
        tvContent.setAlpha(1);
        tvContent.setText(t);
        tvContent.setMovementMethod(LinkMovementMethod.getInstance());
        tvContent.setVisibility(TextUtils.isEmpty(t)?GONE:VISIBLE);
    }

    /**
     * @param t
     */
    public void setText(SpannableStringBuilder t){
        tvContent.setAlpha(1);
        tvContent.setText(t);
        tvContent.setMovementMethod(LinkMovementMethod.getInstance());
        tvContent.setVisibility(TextUtils.isEmpty(t)?GONE:VISIBLE);
    }


    /**
     * 设置图
     * @param i
     */
    public void setIcon(int i,String t){
        icon.setImageResource(i);
        tvContent.setText(t);
        tvContent.setVisibility(TextUtils.isEmpty(t)?GONE:VISIBLE);
    }

    /**
     * 设置图
     * @param i
     */
    public void setIcon(Drawable i,String t){
        icon.setImageDrawable(i);
        tvContent.setText(t);
        tvContent.setVisibility(TextUtils.isEmpty(t)?GONE:VISIBLE);
    }


    /**
     * 设置图
     * @param i
     */
    public void setIcon(Bitmap i,String t){
        icon.setImageBitmap(i);
        tvContent.setText(t);
        tvContent.setVisibility(TextUtils.isEmpty(t)?GONE:VISIBLE);
    }
}
